package com.examonline.service.impl;

import cn.hutool.extra.cglib.CglibUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.examonline.pojo.Paper;
import com.examonline.pojo.dto.PaperDTO;
import com.examonline.pojo.vo.PaperVO;
import com.examonline.service.PaperService;
import com.examonline.mapper.PaperMapper;
import com.examonline.utils.redis.RedisData;
import com.examonline.utils.result.Result;
import com.examonline.utils.result.ResultUtil;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import cn.hutool.core.bean.BeanUtil;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import static com.examonline.utils.redis.RedisConstant.PAPER_QUERY;

/**
* @author dengxiang
* @description 针对表【exam_paper】的数据库操作Service实现
* @createDate 2024-02-02 19:17:35
*/
@Service
public class PaperServiceImpl extends ServiceImpl<PaperMapper, Paper>
    implements PaperService{

    @Resource
    private PaperMapper paperMapper;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public Result queryList(PaperDTO paperDTO) {
        List<Paper> papers = paperMapper.selectPapers(paperDTO);
        List<PaperVO> paperVOS=new ArrayList<>();
        papers.forEach(item->{
            RedisData redisData = new RedisData(item, LocalDate.now());
            String str = JSONUtil.toJsonStr(redisData);
            stringRedisTemplate.opsForList().rightPush(PAPER_QUERY+item.getId(),str);
            PaperVO paperVO = new PaperVO();
            BeanUtil.copyProperties(item,paperVO);
            paperVOS.add(paperVO);
        });

        return ResultUtil.success(paperVOS);
    }
}




